package game;

import javax.microedition.khronos.opengles.GL10;

import challenges.ChainMemory;
import challenges.DontLetUp;
import challenges.NPuzzlePlus;
import challenges.Snowfall;
import challenges.Squishy;
import android.graphics.PointF;
import gfx.Border;
//import com.enso.CoM.entities.Player;
import gfx.Camera;
import gfx.GUI;
import gfx.Spritesheet;
import util.GameVars;
import util.InputHandler;
import util.Logger;

public class Game {
	private static FullscreenActivity activity;
	SplashScreen splash;
	private Menu menu;

//	private Stage currentStage;
	private short currentGame;
	private Challenge challenge;

	private short gameLayer;
	private boolean isReady = false;
	private boolean splashReady = false;
	private boolean close = false;
	
	public Game(){
		splash = new SplashScreen();
		
		menu = new Menu();
		gameLayer = GameVars.GAME_LAYER_SPLASH;
	}

	public void update(long time){
		InputHandler.getInstance().tick();
		
		if(close)
			activity.quickKill();

		if(Spritesheet.getInstance().isLoaded()){
			isReady = true;
		}
		
		if(isReady && splashReady)
			if(gameLayer <= GameVars.GAME_LAYER_MENU)
				if(!menu.isLoaded())
					menu.load();
		
		switch(gameLayer){
		case GameVars.GAME_LAYER_SPLASH:
			if(splash.next()){
				if(GameVars.MASTER_BUILD)
					gameLayer = GameVars.GAME_LAYER_MENU;
				else gameLayer = GameVars.GAME_LAYER_STAGE;
				splash = null;
			}
			else splash.update(time);
			break;
		case GameVars.GAME_LAYER_STAGE:
			if(challenge == null){
//				challenge = new ChainMemory();
//				challenge = new DontLetUp();
//				challenge = new Squishy();
//				challenge = new NPuzzlePlus();
				challenge = new Snowfall();
				menu = null;
			}
			else challenge.update(time);
			if(challenge.back()){
				menu = new Menu();
				challenge = null;
				gameLayer = GameVars.GAME_LAYER_MENU;
				break;
			}
			if(challenge.kill()){
				challenge = null;
				if(GameVars.MASTER_BUILD){
					menu = new Menu();
					gameLayer = GameVars.GAME_LAYER_MENU;
				}
				else close = true;
				break;
			}
//			else challenge.update(time);
			break;
		case GameVars.GAME_LAYER_MENU:
			if(menu.inMenu()){
				menu.update(time);
				close = menu.close();
				break;
			}
			else {
				gameLayer = menu.getNextGameLayer();
				Logger.log("Layer: " + gameLayer);
				break;
			}
		}
	}
	
	public void draw(GL10 gl){
		if(gameLayer == GameVars.GAME_LAYER_SPLASH)
			if(!Spritesheet.getInstance().isSplashLoaded()) return;
			else {
				splash.draw(gl);
				splashReady = true;
			}
		if(!Spritesheet.getInstance().isLoaded()) return;
		
		switch(gameLayer){
//		case GameVars.GAME_LAYER_SPLASH:
//			splash.draw(gl);
//			break;
		case GameVars.GAME_LAYER_STAGE:
			if(challenge == null || !challenge.isLoaded()) break;
			challenge.draw(gl);
			break;
		case GameVars.GAME_LAYER_MENU:
			if(menu.isLoaded())
				menu.draw(gl);
			break;
		}

		Border.drawBorder(gl, 0, 0, 0, 1);
	}
	
	public boolean loadGraphics(GL10 gl){
		
		switch(gameLayer){
		case GameVars.GAME_LAYER_STAGE:
//			if(challenge == null || !challenge.isLoaded()) break;
//			if(!challenge.hasGraphicsLoaded()){
//				challenge.loadGraphics(gl);
//				return false;
//			}
			break;
		case GameVars.GAME_LAYER_MENU:
			break;
		}
		return true;
	}
	
	public Challenge getChallenge(){ return challenge; }
	public boolean isReady(){ return isReady; }
	public void setContext(FullscreenActivity c){ activity = c; }
	public boolean close(){ return close; }
	
	public void pause(){
		
	}
	
	public boolean handleBackPress(){
		switch(gameLayer){
		case GameVars.GAME_LAYER_SPLASH:
			return true;
		case GameVars.GAME_LAYER_STAGE:
			return challenge.handleBackPress();
		case GameVars.GAME_LAYER_MENU:
			return menu.handleBackPress();
		}
		return false;
	}

	public void handleUp(PointF location) {
		switch(gameLayer){
		case GameVars.GAME_LAYER_SPLASH:
			if(menu.isLoaded()){
				splash.handleUp(location);
			}
			break;
		case GameVars.GAME_LAYER_STAGE:
			challenge.handleUp(location);
			break;
		case GameVars.GAME_LAYER_MENU:
			menu.handleUp(location);
			break;
		default:
			break;
		}		
	}

	public void handleDrag(PointF location) {
		switch(gameLayer){
		case GameVars.GAME_LAYER_STAGE:
			challenge.handleDrag(location);
			break;
		case GameVars.GAME_LAYER_MENU:
			menu.handleDrag(location);
			break;
		default:
			break;
		}		
	}

	public void handleTouch(PointF location) {
		switch(gameLayer){
		case GameVars.GAME_LAYER_STAGE:
			challenge.handleTouch(location);
			break;
		case GameVars.GAME_LAYER_MENU:
			menu.handleTouch(location);
			break;
		default:
			break;
		}		
	}
}
